﻿@model Controller2.Models.Campanha
@{
    Layout = null;
}
<!DOCTYPE html>
<html class="no-js">
<head>
    <title>Cadastro</title>
    @if (!Request.IsAjaxRequest())
    {
        <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
        <link href="../../Content/themes/controller/controller.css" rel="stylesheet" type="text/css" />
        @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))  
        <script src="@Url.Content("~/Scripts/jquery.min.js")" type="text/javascript"></script>
        <script src="@Url.Content("~/Scripts/jquery-ui.js")" type="text/javascript"></script>    
        <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>   
        <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script> 
    }
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/controller.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/numbers.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.maskedinput-1.3.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
    @(Html.Telerik().StyleSheetRegistrar().DefaultGroup(group => group.Add("telerik.common.css").Add("telerik.vista.css").Combined(true).Compress(true)))
    <style type="text/css">
        .editor-label
        {
            width: 120px;
        }
        #NomeCliente
        {
            width: 343px;
        }
        #IdCliente
        {
            width: 50px;
            text-align: right;
        }
        #btnCliente
        {
            height: 21px;
            margin: 0;
            padding: 0 5px 20px 5px;
            line-height: 21px;
            vertical-align: top;
        }
    </style>
    @if (false)
    {
        <script src="../../Scripts/jquery-vsdoc.js" type="text/javascript"></script>
        <script src="../../Scripts/controller.js" type="text/javascript"></script>
    }
</head>
<body id="frame">
    <div id="formHide">
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)
            <fieldset>
                <legend>Campanha</legend>
                @Html.HiddenFor(model => model.IdCampanha)
                <div class="editor-label">
                    @Html.LabelFor(model => model.DataCadastro, "Data Cadastro: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.DataCadastro)
                    @Html.ValidationMessageFor(model => model.DataCadastro)
                    @Html.LabelFor(model => model.DataInicio, "Dt. Início: ")
                    @Html.EditorFor(model => model.DataInicio)
                    @Html.ValidationMessageFor(model => model.DataInicio)
                    @Html.LabelFor(model => model.DataFim, "Dt. Fim: ")
                    @Html.EditorFor(model => model.DataFim)
                    @Html.ValidationMessageFor(model => model.DataFim)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Nome, "Nome: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Nome)
                    @Html.ValidationMessageFor(model => model.Nome)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.Descricao, "Descrição: ")
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Descricao)
                    @Html.ValidationMessageFor(model => model.Descricao)
                </div>
                <div class="editor-label">
                    @Html.LabelFor(model => model.IdCliente, "Cliente: ")
                </div>
                <div class="editor-field">
                    @Html.TextBoxFor(model => model.IdCliente, new { @readonly = "readonly" })
                    @Html.TextBox("NomeCliente", Model.Cliente != null ? Model.Cliente.NomePessoa : string.Empty, new { @readonly = "readonly" })
                    @Html.ValidationMessageFor(model => model.IdCliente)
                    @if (Model.IdCampanha <= 0)
                    {
                        <input type="button" id="btnCliente" value="..." />
                    }
                </div>
            </fieldset>
        
            <div id="botoesForm">
                @if (!((Boolean)ViewBag.IsCliente || (Boolean)ViewBag.IsFunc))
                {
                    <input type="submit" id="Gravar" value="Gravar" />                    
                }
                
                <input type="button" id="Fechar" value="Cancelar" />
            </div>
        }
    </div>
    <div id="alertaErro" style="display: none">
    </div>
    <div id="winFormPesquisaCliente" style="display: none">
    </div>
   @if (!User.IsInRole("Cliente"))
   {
       <script type="text/javascript">
           $(function () {
               $('.datepicker').each(function () {
                   $(this).rules("add", {
                       dataValidate: true,
                       validaDataMaior: true,
                       messages: {
                           dataValidate: "*",
                           validaDataMaior: "*"
                       }
                   });
               });
               if ($('#IdCliente').val() == 0) {
                   $('#IdCliente').val('');
               }
               $('.datepicker').mask("99/99/9999");
               $('#formHide').fadeIn('slow');
               hideLoading();
               $('#Nome').focus();
           });
           $('form').submit(function (e) {
               e.preventDefault();
               if ($('form').valid()) {
                   $.post('@Url.Action("Salvar", "Campanha")', $('form').serialize(), function (data) {
                       $('#Gravar').attr('disabled', false);
                       if (data.salvo) {
                           closeWindow();
                       } else {
                           alertError(data.error);
                       }
                   });
               }
           });
           $.validator.methods.dataValidate = function (value, element, param) {
               return validaData(element);
           };
           $.validator.methods.validaDataMaior = function (value, element, param) {
               return validaDataMaior($("#DataCadastro"), $("#DataInicio")) && validaDataMaior($("#DataCadastro"), $("#DataFim")) && validaDataMaior($("#DataInicio"), $("#DataFim"));
           };
           $('#Fechar').click(function () { closeWindow(); });
           $('#btnCliente').click(function () {
               showLoading(); var url = '@Url.Action("PesquisaCliente", "Cliente")'; $('#winFormPesquisaCliente').empty().load(url); formDialog('winFormPesquisaCliente', 'Pesquisa de Clientes', 460, 700); return false;
           });
           function preencherCamposPesquisaCliente(idCliente) {
               $.post('@Url.Action("BuscaNomeCliente", "Cliente")', { id: idCliente }, function (data) {
                   $('#IdCliente').val(idCliente);
                   $('#NomeCliente').val(data);
               });
               hideLoading();
           }
           function closeWindowPesquisa() {
               $('#winFormPesquisaCliente').dialog('close');
               $('#winFormPesquisaCliente').removeData();
           }
       </script>
   }
   else
   {
       <script type="text/javascript">
           $(function () {               
               $('#formHide').fadeIn('slow');
               hideLoading();               
           });           
           $('#Fechar').click(function () { closeWindow(); });           
       </script>
   }
</body>
</html>
